var query = require("../config/conn.js");
module.exports = {
//获取/查找用户
getAlluserList(params,cb){
  var _sql = "select * from blogger " ;

  query(_sql,params||[],function(err,result){
	if(!params){
    cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  query(_sql,params||[],function(err,result){
   cb(JSON.stringify({msg:"success",data:result}));
  })
  })
},
//登录获取用户信息
getlogin(id){
  return new Promise(function(resolve,reject){
     var _sql = null;
     _sql = "select id, wx_img,nickname,userimg,gender,email,profession,age,instroduction from blogger where id= ? " ;
    query(_sql,id,function(err,result){
      if(err||result.length==0){ resolve({status:"error",msg:err||'没有数据'});}
      resolve({status:"success",data:result[0]});
    })
  })
 
  
},
//查找博客列表
  findblog(bloggerid){
  var _sql = null;
  return new Promise(function(resolve,reject){
     if(typeof bloggerid != 'undefined'){
        _sql = "select * from blog  where bloggerid = ? " ;
     }else{
        _sql = "select * from blog " ;
     }
    query(_sql,bloggerid,function(err,result){
      if(err||result.length==0){ resolve({status:"error",msg:err||'没有数据'});}
      resolve({status:"success",data:result});
    })
  })
  },
  //查找博客详情
  getblogDetails(blogid){
    var _sql = null;
  return new Promise(function(resolve,reject){
     
    _sql = "select * from blog where blogid = ? " ;
    query(_sql,blogid,function(err,result){
      if(err||result.length==0){ resolve({status:"error",msg:err||'没有数据'});}
      resolve({status:"success",data:result[0]});
    })
  })
},
//注册
getreg(params,cb){
  var _sql = "insert into blogger(username,password,gender,age,profession,telephone,email,instroduction,createtime,updatatime) values(?,?,?,?,?,?,?,?,?,?)" ;
  if(!params){
  	cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  //[{"id":0,"username":"root","password":"root","gender":"男","age":"18","profession":"it","telephone":"17611152709","email":"1070156025@qq.com","instroduction":"本人","createtime":"201801","updatatime":"2018"}]

  query(_sql,params||[],function(err,result){
  	if(err){
  	cb(JSON.stringify({msg:"error",data:null}));
    return;
     }else{
	 cb(JSON.stringify({msg:"success",data:result}));
     }
	 
  })
},
//查找博客列表
getblog(params,cb){
  var _sql = null;
  if(params.length==1){
    _sql = "select * from blog limit ?,5";
  }
  if(params.length==2){
   _sql = "select * from blog  where bloggerid = ?   order by blogid desc limit ?,5" ;
  }
  if(params.length==0){
    cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  query(_sql,params||[],function(err,result){
   cb(JSON.stringify({msg:"success",data:result}));
  })
},
getpages(params,cb){
  var _sql = "select count(*) count from blog  " ;
  if(!params){
    cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  query(_sql,params||[],function(err,result){
   cb(JSON.stringify({msg:"success",data:result}));
  })
},

//增加博客列表
addblog(params,cb){
  var _sql = "insert into blog(title,bloginfo,readtimes,kinds,content,createtime,updatetime,headimg,bloggerid) values(?,?,?,?,?,?,?,?,?)";
  if(!params){
    cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  query(_sql,params||[],function(err,result){
   cb(JSON.stringify({msg:"success",data:result}));
  })

},
//删除博客
deleteblog(params,cb){
   var _sql = "delete  from blog where blogid = ?" ;
  if(!params){
    cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  query(_sql,params||[],function(err,result){
   cb(JSON.stringify({msg:"success",data:result}));
  })
},
//后台管理人员
getadmin(params,cb){
   var _sql = "select * from username where userName = ? and password = ?" ;
  if(!params){
    cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  query(_sql,params||[],function(err,result){
   cb(JSON.stringify({msg:"success",data:result}));
  })
},
//修改文章的列表
modifyArticle(params,cb){
   var _sql = "UPDATE  blog set title= ? , bloginfo = ?,readtimes=?,kinds=?, content=? ,createtime=?, updatetime= ? ,headimg = ? WHERE  blogid =  ? ;" ;
  if(!params){
    cb(JSON.stringify({msg:"error",data:null}));
    return;
  }
  query(_sql,params||[],function(err,result){
   cb(JSON.stringify({msg:"success",data:result}));
  })
},
}

//字段  blogid title  bloginfo readtimes kinds content createtime updatetime
